package com.jk.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import sun.security.provider.SecureRandom;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

public class PayUtils<out_trade_no> {
  @Autowired
  private  RedisTemplate redisTemplate;
    private final String APP_ID = "2021000117629775";
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0pHWQENa2DA3JVHAt4l8hJCviIdi+MvE8SuBwYG5502atBflmozQBqPuWzsAETIdRURujt6dodYL8NStC7hVpWu1iv4ueVFeUvaOkzMqh2nFX20BbHVrEKwDJNIlclFV05vVB3LZTt74FSv6US1veREAfxpYVFejT+3diroNQqI3swXg2ySOmAbYOBgPTGFHFMRG1VQH8iyDAnU20lDi1PsBvkAYMNNmiS1R7AqVzhpZe730e/qnrQ6TV63KKBbX5O0UV3qz6ewmMBbEpzajZo6zwReuU/bZJG0gzHU0qgtK7C2eU2HBohyOZTlDXuNv2nSpmyq68yVHW12AlUG17AgMBAAECggEAVUrRt5Q7zPkkCUu1K/UWta8+iScyD5w+CxnPnz/o6dzHPirz92JXy/Dd2fcE8wKvYdXKFyNACDJ0BmGUa6W+tmAYdee7JKbuM2zXN5j6lDFwEuuTF3JkyPsFA/uqX+wp7WifpNlxvVWkgxtF2P88AVZ4cIhzlMEjjfPOCuobVpl0VV/0nSgYSLlIpLrvx7uVHXP8ciMTWMfo1tH/6lX5Vf1DP8Sbnhzzxz4BrPyryi9TK4dXKwNKUfj+bevu8fRH29Wxuc7h+rF9uGv7VMMlW/63RDGO3Qn8IjMGoZ2JCO1qlQI5RhyxARmm8s33YDFllq95bdQiz7cnW6enLxBcAQKBgQDYejVOIlZjaXb2FdQZReqnYPS/X6dILxPdWAl/H8BZDN36cUNUO4NlEqmfm2Vi9o4DeYblOLBDxYBokfbUSWCvhvL/p/bGFRvrAGdHDCELMykppG/xsAnB0Ud3g6b/ufhmrn8rTInbpgB0HgoeuQAwysBmA5yMq9kzq9vTJ22biwKBgQDVn2KEpMG6zXjebjzdjqw8UaD4zZutqLTu99en9c4E4vZnxZS9tLFOq0gm4kZ8aKBwQhyXe6nSP63runi/XK4T0Ix2AzZMebpAWYDdiOezXaRyVDwBtzZ10sVyRL0p6Vbm3dkn1Cu1mv4D1E87AP+R9Fu1DhZvEpOIs3Zdan9z0QKBgQCPQcCI5nxY6XBKRgPIfdZd0fHlu31Obaktr5cJQ4/c3oD/vXibA49ekc0izIl5xkQQ1JmKk8tB7i+eBApeTfjn79xbh2HtkAabq/ass8AiU0cIQcO7I44xbzdmRf97+CwEd7G8i7gnXWGrdj2oOzF/GI3edMFi7k6cvTyKLeuBJQKBgC9LCveMtisSBDlSNH1kcbMk6ztJ4bZPjEb/baoMnCCu0phkwKpDD6H3/5BENhCi5SoVKNlMkmsul+WBMexDq3JNkFssGOBTbq2CYyoofgtSpu58wZArrI3nLLlAiJR3yTpxUPslWMvq1WOppCTO8sWwUIFD7imG9kykVocV5cORAoGAWHcVP9P4KI7YXD4QIK0YqQp0fNtN/Vt23JAji86YnkW266apBCTx5J1ioMGhm/id/AnOMpqTWLRvi58VQEUUXw7hVgNNo5il+7TX+lE9STp2+DhDERguwbp4KLMBlXsu4fHJX5Tpp/7Qnl2lRMeVkfV2c3CEnvTX1oiC4aoC8u4=";
    private final String CHARSET = "UTF-8";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlWHRrmFcZI/na1UNCIOBRAarCxA42EkKPFdSpnXTRkdWVDrb19dqHfyOP8i/MRidwoHGpVvmvuUPME3yKgvHsJc2EiStUJAROrQoTrqmdikfWwuXEwim6gw2zFrslQfou0ePkk/tP6rO751uhDvRjcZIc3nHDrb7qOyVClRLDi13rlxul3iwuectG/2S6FVPrcXQ0srQpIocvDqRlJP8xYdl91tDHWiOMdVOcLKNVL8/j5AAC68DurO1RghQR4ShJ6dvWCJx1JSZ32ZgsSzySZQxKkuHYeXRRfxAhOaSKCdUAh3Q6WBPw8i1TaPV5lTXU2zIjbwSUyuq6dZJX6l91QIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
   private final String NOTIFY_URL = "http://127.0.0.1/";



   //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:9999/cart/pay";



    public void alipay(HttpServletResponse httpResponse, Double money, String name, String info ) throws IOException {

        SecureRandom r= new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
     String  out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Double.toString(money);
        //订单名称，必填
        String subject =name;
        //商品描述，可空
        String body = info;
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
       httpResponse.setContentType("text/html;charset=" + CHARSET);

        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }




}
